package com.baobao.service;

import com.baobao.dao.ParentDAO;

import com.baobao.domain.Parent;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Parent entities
 * 
 */

@Service("ParentService")
@Transactional
public class ParentServiceImpl implements ParentService {

	/**
	 * DAO injected by Spring that manages Parent entities
	 * 
	 */
	@Autowired
	private ParentDAO parentDAO;

	/**
	 * Instantiates a new ParentServiceImpl.
	 *
	 */
	public ParentServiceImpl() {
	}

	/**
	 * Load an existing Parent entity
	 * 
	 */
	@Transactional
	public Set<Parent> loadParents() {
		return parentDAO.findAllParents();
	}

	/**
	 * Return a count of all Parent entity
	 * 
	 */
	@Transactional
	public Integer countParents() {
		return ((Long) parentDAO.createQuerySingleResult("select count(o) from Parent o").getSingleResult()).intValue();
	}

	/**
	 * Return all Parent entity
	 * 
	 */
	@Transactional
	public List<Parent> findAllParents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Parent>(parentDAO.findAllParents(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Parent findParentByPrimaryKey(String phone) {
		return parentDAO.findParentByPrimaryKey(phone);
	}
	
	@Transactional
	public Set<Parent> findParentByBbid(String bbId) {
		Set<Parent> parent1 =  parentDAO.findParentByBbId1(bbId);
		Set<Parent> parent2 =  parentDAO.findParentByBbId2(bbId);
		Set<Parent> parent3 =  parentDAO.findParentByBbId3(bbId);
		Set<Parent> parent4 =  parentDAO.findParentByBbId4(bbId);
		Set<Parent> parent5 =  parentDAO.findParentByBbId5(bbId);
		
		parent1.addAll(parent2);
		parent1.addAll(parent3);
		parent1.addAll(parent4);
		parent1.addAll(parent5);
		return parent1;
	}

	/**
	 * Save an existing Parent entity
	 * 
	 */
	@Transactional
	public void saveParent(Parent parent) {
		Parent existingParent = parentDAO.findParentByPrimaryKey(parent.getPhone());

		if (existingParent != null) {
			if (existingParent != parent) {
				existingParent.setPhone(parent.getPhone());
				existingParent.setName(parent.getName());
				existingParent.setAccount(parent.getAccount());
				existingParent.setPassword(parent.getPassword());
				existingParent.setRelation(parent.getRelation());
				existingParent.setBbId1(parent.getBbId1());
				existingParent.setBbId2(parent.getBbId2());
				existingParent.setBbId3(parent.getBbId3());
				existingParent.setBbId4(parent.getBbId4());
				existingParent.setBbId5(parent.getBbId5());
			}
			parent = parentDAO.store(existingParent);
		} else {
			parent = parentDAO.store(parent);
		}
		parentDAO.flush();
	}

	/**
	 * Delete an existing Parent entity
	 * 
	 */
	@Transactional
	public void deleteParent(Parent parent) {
		parentDAO.remove(parent);
		parentDAO.flush();
	}
}
